package org.zhb.sql.query.sys.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.zhb.sql.query.core.common.utils.BeanUtils;
import org.zhb.sql.query.core.constant.RetCode;
import org.zhb.sql.query.core.util.EncryptionUtils;
import org.zhb.sql.query.core.util.JsonReturn;
import org.zhb.sql.query.core.util.ValidResult;
import org.zhb.sql.query.model.Role;
import org.zhb.sql.query.model.User;
import org.zhb.sql.query.sys.service.RoleService;
import org.zhb.sql.query.sys.service.UserService;

/**
 * @ClassName: Usercontroller。
 * @Description: User管理Controller
 * @author: zhb
 *
 */
@RestController
@RequestMapping("/user/")
public class UserController {
	Logger log = LoggerFactory.getLogger(UserController.class);
	@Autowired
	private UserService userService;
	@Autowired
	private RoleService roleService;

	@RequestMapping("userInfo")
	public ModelAndView userInfo() {
		return new ModelAndView("shiro/user/userInfo");
	}

	@RequestMapping("userList")
	public JsonReturn userList() {
		JsonReturn jsonReturn = new JsonReturn();
		Page<User> page = userService.selectModelByCondition(new PageRequest(0, 20));
		log.info("用户查询成功!!!");
		jsonReturn.setRetcode(RetCode.OK_CODE);
		jsonReturn.setRetmsg(page);
		return jsonReturn;
	}

	/**
	 * @Title: shareRole
	 * @Description: 跳转到分配角色页面
	 * @return: ModelAndView
	 * @param: @return
	 * @author: zhb
	 * @throws 2016年4月13日上午10:48:38
	 */
	@RequestMapping("shareRole")
	public ModelAndView shareRole() {
		// 左侧列表查询所有用户,右侧查询所有角色
		List<User> userList = userService.findAll(new User());
		List<Role> roleList = roleService.findAll(new Role());
		return new ModelAndView("shiro/user/shareRole").addObject("users", userList).addObject("roles", roleList);
	}

	/**
	 * 
	 * @Title: allocationRole
	 * @Description: 分配角色
	 * @return: ModelAndView
	 * @param: @return
	 * @author: zhb
	 * @throws 2016年4月13日上午10:50:43
	 */
	@RequestMapping("allocationRole")
	public JsonReturn allocationRole() {
		JsonReturn jsonReturn = new JsonReturn();
		return jsonReturn;
	}

	@RequestMapping("insert")
	public JsonReturn insert(User user) {
		JsonReturn jsonReturn = new JsonReturn();
		user.setPassword(EncryptionUtils.getEncryptionPassword(user.getPassword()));
		try {
			userService.save(user);
			log.info("用户保存成功!!!");
			jsonReturn.setRetcode(RetCode.OK_CODE);
			jsonReturn.setRetmsg("添加成功");
		} catch (Exception e) {
			jsonReturn.setRetcode(RetCode.ERROR_CODE);
			jsonReturn.setRetmsg("添加失败");
			log.info("用户保存成功!!!" + e.getMessage());
		}
		return jsonReturn;
	}

	@RequestMapping("checkUserName")
	public ValidResult checkUserName(String userName) {
		User user = userService.selectByUserName(userName);
		ValidResult result = new ValidResult();
		if (user != null) {
			log.info("用户查询成功!!!");
			result.setValid("false");
			return result;
		}
		result.setValid("true");
		return result;
	}

	@RequestMapping("checkPhone")
	public ValidResult checkPhone(String phone) {
		User user = userService.findByPhone(phone);
		ValidResult result = new ValidResult();
		if (user != null) {
			log.info("用户查询成功!!!");
			result.setValid("false");
			return result;
		}
		result.setValid("true");
		return result;
	}

	@RequestMapping("checkEmail")
	public ValidResult checkEmail(String email) {
		User user = userService.findByEmail(email);
		ValidResult result = new ValidResult();
		if (user != null) {
			log.info("用户查询成功!!!");
			result.setValid("false");
			return result;
		}
		result.setValid("true");
		return result;
	}

	@RequestMapping("selectUserById")
	public JsonReturn selectUserById(String id) {
		JsonReturn jsonReturn = new JsonReturn();
		try {
			User queryUser = userService.findById(id);
			jsonReturn.setRetcode(RetCode.OK_CODE);
			jsonReturn.setRetmsg(queryUser);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return jsonReturn;
	}

	@RequestMapping("edit")
	public JsonReturn edit(User user) {
		JsonReturn jsonReturn = new JsonReturn();
		User target = userService.findById(user.getId());
		BeanUtils.copyProperties(user, target);
		try {
			userService.update(target);
			log.info("用户更新成功!!!");
			jsonReturn.setRetcode(RetCode.OK_CODE);
			jsonReturn.setRetmsg("更新成功");
		} catch (Exception e) {
			log.info("用户更新失败!!!" + e.getMessage());
			jsonReturn.setRetcode(RetCode.ERROR_CODE);
			jsonReturn.setRetmsg("更新失败");
		}
		return jsonReturn;
	}

	@RequestMapping("delete")
	public JsonReturn delete(String id) {
		JsonReturn jsonReturn = new JsonReturn();
		try {
			userService.delete(id);
			log.info("删除成功");
			jsonReturn.setRetcode(RetCode.OK_CODE);
			jsonReturn.setRetmsg("删除成功!");
		} catch (Exception e) {
			log.info("删除失败" + e.getMessage());
			jsonReturn.setRetcode(RetCode.ERROR_CODE);
			jsonReturn.setRetmsg("删除失败!");
			e.printStackTrace();
		}
		return jsonReturn;
	}

	/**
	 * 修改密码
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("updatePassword")
	@Transactional
	public JsonReturn updatePassword(HttpServletRequest request, String password, String newPassword,
			String confirmPassword) {
		JsonReturn jsonReturn = new JsonReturn();
		String userName = (String) request.getSession().getAttribute("userName");
		String oldPassword = (String) request.getSession().getAttribute("password");
		if (!password.equals(oldPassword)) {
			jsonReturn.setRetcode(RetCode.ERROR_CODE);
			jsonReturn.setRetmsg("原始密码输入错误!");
			return jsonReturn;
		}
		if (!newPassword.equals(confirmPassword)) {
			jsonReturn.setRetcode(RetCode.ERROR_CODE);
			jsonReturn.setRetmsg("两次输入密码不一致!");
			return jsonReturn;
		}
		try {
			userService.updatePasswordByUserName(EncryptionUtils.getEncryptionPassword(newPassword), userName);
			log.info("修改密码成功");
			jsonReturn.setRetcode(RetCode.OK_CODE);
			jsonReturn.setRetmsg("修改密码成功!");
		} catch (Exception e) {
			log.info("修改密码失败" + e.getMessage());
			jsonReturn.setRetcode(RetCode.ERROR_CODE);
			jsonReturn.setRetmsg("修改 密码失败!");
			e.printStackTrace();
		}
		return jsonReturn;
	}
}
